// pages/Addaddress/index.js
import  areaList  from '../../utils/area.js';
import ddt from '../../api/axios.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    name:'',
    phone:'',
    address:'',
    desaddress:'',
    moren:false,
    show:false,
    addresslist:[],
    defaultadd:2,
    userinfo:'',
    addresscode:'',
  },
  showtrue(){
    this.setData({
      show:true
    })
  },
  commit(){
    var llist=[
      this.data.userinfo.user_id,
     this.data.name ,
      this.data.phone,
     this.data.addresslist[0],
      this.data.desaddress,

    ]
    if(llist.includes(undefined) || llist.includes('')){
      wx.showToast({
        title: '请填写信息',
        icon:'error'
      })
      return
   }
     ddt('post','/api/user/editaddress',{
      user_name:this.data.name ,
      addressid: '',
      phone: this.data.phone,
      province:this.data.addresslist[0].name,
      city: this.data.addresslist[1].name,
      district: this.data.addresslist[2].name,
      address: this.data.desaddress,
      defaultadd: this.data.defaultadd,
      code: this.data.addresscode, 
     }).then(res=>{
       if(res.data.code == 1){
         wx.showToast({
           title: '保存成功',
           icon:'success'
         })
         wx.navigateBack({})
       }else{
        wx.showToast({
          title: res.data.msg,
          icon:'error'
        })
       }
     })
  },
  onChange(e){
    this.setData({
      name:e.detail
    })
  },
  onChange1(e){
    this.setData({
      phone:e.detail
    })
  },
  onChange2(e){
    this.setData({
      desaddress:e.detail.value
    })
  },
  confirm(e){
 console.log(e)
    var aa  =e.detail.values[0].name+'-' +e.detail.values[1].name+'-' +e.detail.values[2].name
   this.setData({
     addresslist:e.detail.values,
     addresscode : e.detail.values[2].code,
     address:aa,
      show:false
   })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
      radio(event){
     if(event.detail == true){
      this.setData({
        defaultadd:1
      })
     }else{
      this.setData({
        defaultadd:2
      })
     }
    this.setData({
      moren: event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        areaList
      })
      console.log(
        this.data.areaList
      )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userinfo:wx.getStorageSync('userinfo')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})